package com.gqj.meter.service.impl;

import com.gqj.common.constant.UserConstants;
import com.gqj.common.exception.ServiceException;
import com.gqj.common.utils.StringUtils;
import com.gqj.common.utils.bean.BeanValidators;
import com.gqj.meter.domain.MdOtherMaterial;
import com.gqj.meter.mapper.MdOtherMaterialMapper;
import com.gqj.meter.service.IMdOtherMaterialService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;
import java.util.Map;

/**
 * 其他物资Service业务层处理
 *
 * @author lxq
 * @date lxq-02-10
 */
@Service
public class MdOtherMaterialServiceImpl implements IMdOtherMaterialService {
    @Autowired
    private MdOtherMaterialMapper mdOtherMaterialMapper;
    private static final Logger log = LoggerFactory.getLogger(MdOtherMaterialServiceImpl.class);
    @Autowired
    protected Validator validator;

    /**
     * 查询其他物资
     *
     * @param otherMaterialId 其他物资主键
     * @return 其他物资
     */
    @Override
    public MdOtherMaterial selectMdOtherMaterialByOtherMaterialId(Long otherMaterialId) {
        return mdOtherMaterialMapper.selectMdOtherMaterialByOtherMaterialId(otherMaterialId);
    }

    /**
     * 查询其他物资列表
     *
     * @param mdOtherMaterial 其他物资
     * @return 其他物资
     */
    @Override
    public List<MdOtherMaterial> selectMdOtherMaterialList(MdOtherMaterial mdOtherMaterial) {
        return mdOtherMaterialMapper.selectMdOtherMaterialList(mdOtherMaterial);
    }

    /**
     * 新增其他物资
     *
     * @param mdOtherMaterial 其他物资
     * @return 结果
     */
    @Override
    public int insertMdOtherMaterial(MdOtherMaterial mdOtherMaterial) {
        return mdOtherMaterialMapper.insertMdOtherMaterial(mdOtherMaterial);
    }

    /**
     * 修改其他物资
     *
     * @param mdOtherMaterial 其他物资
     * @return 结果
     */
    @Override
    public int updateMdOtherMaterial(MdOtherMaterial mdOtherMaterial) {
        return mdOtherMaterialMapper.updateMdOtherMaterial(mdOtherMaterial);
    }

    /**
     * 批量删除其他物资
     *
     * @param otherMaterialIds 需要删除的其他物资主键
     * @return 结果
     */
    @Override
    public int deleteMdOtherMaterialByOtherMaterialIds(Long[] otherMaterialIds) {
        return mdOtherMaterialMapper.deleteMdOtherMaterialByOtherMaterialIds(otherMaterialIds);
    }

    /**
     * 删除其他物资信息
     *
     * @param otherMaterialId 其他物资主键
     * @return 结果
     */
    @Override
    public int deleteMdOtherMaterialByOtherMaterialId(Long otherMaterialId) {
        return mdOtherMaterialMapper.deleteMdOtherMaterialByOtherMaterialId(otherMaterialId);
    }

    /**
     * 校验设备条形码是否唯一
     *
     * @param mdOtherMaterial 其他物资
     * @return 结果
     */
    public String checkBarCodeUnique(MdOtherMaterial mdOtherMaterial) {
        Long otherMaterialId = StringUtils.isNull(mdOtherMaterial.getOtherMaterialId()) ? -1L : mdOtherMaterial.getOtherMaterialId();
        MdOtherMaterial info = mdOtherMaterialMapper.checkBarCodeUnique(mdOtherMaterial.getBarCode());
        if (StringUtils.isNotNull(info) && info.getOtherMaterialId().longValue() != otherMaterialId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 导入其他物资数据
     *
     * @param mdOtherMaterialList 其他物资列表
     * @param isUpdateSupport     是否更新支持，如果已存在，则进行更新数据
     * @param operName            操作用户
     * @return 结果
     */
    public String importOtherMaterial(List<MdOtherMaterial> mdOtherMaterialList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(mdOtherMaterialList) || mdOtherMaterialList.size() == 0) {
            throw new ServiceException("导入其他物质不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MdOtherMaterial otherMaterial : mdOtherMaterialList) {
            try {
                BeanValidators.validateWithException(validator, otherMaterial);
                // 验证是否存在这个数据
                MdOtherMaterial m = mdOtherMaterialMapper.selectMdOtherMaterialByBarCode(otherMaterial.getBarCode());
                if (StringUtils.isNull(m)) {
                    otherMaterial.setServiceStatus("00");
                    this.insertMdOtherMaterial(otherMaterial);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、其他物资 " + otherMaterial.getBarCode() + " 导入成功");
                } else if (isUpdateSupport) {
                    otherMaterial.setOtherMaterialId(m.getOtherMaterialId());
                    this.updateMdOtherMaterial(otherMaterial);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、其他物资 " + otherMaterial.getBarCode() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、其他物资 " + otherMaterial.getBarCode() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、其他物资 " + otherMaterial.getBarCode() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    //查询业务状态数量
    @Override
    public int[] getServiceStatusNum() {
        Map<String, Object> map = mdOtherMaterialMapper.getServiceStatusNum();

        int[] counts = new int[6];
        counts[0] = ((Long) map.get("count_00")).intValue();
        counts[1] = ((Long) map.get("count_01")).intValue();
        counts[2] = ((Long) map.get("count_02")).intValue();
        counts[3] = ((Long) map.get("count_03")).intValue();
        counts[4] = ((Long) map.get("count_04")).intValue();
        counts[5] = ((Long) map.get("count_05")).intValue();
        return counts;
    }
}
